package com.xh3140.xwidget.app.sliderbar;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.Nullable;

public class DoubleSliderBar extends AbstractSliderBar {
  // 值，必须在范围下限与上限之间
  private float mValueLower = 30F;
  private float mValueUpper = 60F;

  // 激活状态，当用户按下滑块时，处于激活状态
  private boolean mActivatedLower;
  private boolean mActivatedUpper;

  // 触摸事件
  @Nullable
  private MotionEvent mMotionEvent;

  // 进度改变监听器
  @Nullable
  private OnValueChangedListener mOnValueChangedListener;

  public DoubleSliderBar(Context context) {
    super(context);
  }

  public DoubleSliderBar(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
  }

  public DoubleSliderBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  public DoubleSliderBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // 滑块横坐标
    final float blockLowerX = mSliderStripRectF.left + v2d(mValueLower - mRangeLower);
    final float blockUpperX = mSliderStripRectF.left + v2d(mValueUpper - mRangeLower);
    // 绘制滑条(普通状态)
    mPaint.setColor(mSliderStripColor);
    if (mValueLower > mRangeLower) {
      canvas.save();
      canvas.clipRect(mSliderStripRectF.left, mSliderStripRectF.top, blockLowerX, mSliderStripRectF.bottom);
      if (mSliderStripRadius > 0) {
        canvas.drawRoundRect(mSliderStripRectF, mSliderStripRadius, mSliderStripRadius, mPaint);
      } else {
        canvas.drawRect(mSliderStripRectF, mPaint);
      }
      canvas.restore();
    }
    if (mValueUpper < mRangeUpper) {
      canvas.save();
      canvas.clipRect(blockUpperX, mSliderStripRectF.top, mSliderStripRectF.right, mSliderStripRectF.bottom);
      if (mSliderStripRadius > 0) {
        canvas.drawRoundRect(mSliderStripRectF, mSliderStripRadius, mSliderStripRadius, mPaint);
      } else {
        canvas.drawRect(mSliderStripRectF, mPaint);
      }
      canvas.restore();
    }
    // 绘制滑条(覆盖状态)
    mPaint.setColor(mSliderBlockColor);
    if (mValueLower < mValueUpper) {
      canvas.save();
      canvas.clipRect(blockLowerX, mSliderStripRectF.top, blockUpperX, mSliderStripRectF.bottom);
      if (mSliderStripRadius > 0) {
        canvas.drawRoundRect(mSliderStripRectF, mSliderStripRadius, mSliderStripRadius, mPaint);
      } else {
        canvas.drawRect(mSliderStripRectF, mPaint);
      }
      canvas.restore();
    }
    // 绘制滑块
    mPaint.setColor(mSliderBlockColor);
    if (mActivatedLower) {
      mActivatedUpper = false;
      canvas.drawCircle(blockLowerX, mSliderBlockCenter, 1.5f * mSliderBlockRadius, mPaint);
    } else {
      canvas.drawCircle(blockLowerX, mSliderBlockCenter, mSliderBlockRadius, mPaint);
    }
    if (mActivatedUpper) {
      mActivatedLower = false;
      canvas.drawCircle(blockUpperX, mSliderBlockCenter, 1.5f * mSliderBlockRadius, mPaint);
    } else {
      canvas.drawCircle(blockUpperX, mSliderBlockCenter, mSliderBlockRadius, mPaint);
    }
  }

  @Override
  public boolean performClick() {
    return super.performClick();
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        final float blockLowerX = mSliderStripRectF.left + v2d(mValueLower - mRangeLower);
        final float blockUpperX = mSliderStripRectF.left + v2d(mValueUpper - mRangeLower);
        final float process = mRangeLower + d2v(event.getX() - mSliderStripRectF.left);
        if (Math.abs(event.getX() - blockLowerX) < Math.abs(event.getX() - blockUpperX)) {
          mActivatedLower = true;
          mActivatedUpper = false;
          setValueLower(constraintInRange(process));
        } else {
          mActivatedLower = false;
          mActivatedUpper = true;
          setValueUpper(constraintInRange(process));
        }
        mMotionEvent = MotionEvent.obtain(event);
        invalidate();
        break;
      case MotionEvent.ACTION_MOVE:
        if (mMotionEvent != null) {
          final float distance = event.getX() - mMotionEvent.getX();
          if (distance == 0F) return false;
          if (mActivatedLower) {
            mActivatedUpper = false;
            if (distance < 0F && mValueLower <= mRangeLower) return false;
            if (distance > 0F && mValueLower >= mRangeUpper) return false;
            final float processLower = mValueLower + d2v(distance);
            if (distance > 0 && processLower > mValueUpper) {
              mActivatedLower = false;
              mActivatedUpper = true;
              setValues(constraintInRange(mValueUpper), constraintInRange(processLower));
            } else {
              setValueLower(constraintInRange(processLower));
            }
          }
          if (mActivatedUpper) {
            mActivatedLower = false;
            if (distance < 0F && mValueUpper <= mRangeLower) return false;
            if (distance > 0F && mValueUpper >= mRangeUpper) return false;
            final float processUpper = mValueUpper + d2v(distance);
            if (distance < 0 && processUpper < mValueLower) {
              mActivatedLower = true;
              mActivatedUpper = false;
              setValues(constraintInRange(processUpper), constraintInRange(mValueLower));
            } else {
              setValueUpper(constraintInRange(processUpper));
            }
          }
          if (mActivatedLower || mActivatedUpper) {
            mMotionEvent = MotionEvent.obtain(event);
            invalidate();
          }
        }
        break;
      case MotionEvent.ACTION_UP:
        performClick();
        mActivatedLower = false;
        mActivatedUpper = false;
        if (mMotionEvent != null) {
          mMotionEvent.recycle();
          mMotionEvent = null;
        }
        invalidate();
        break;
    }
    return true;
  }

  /**
   * 获取低值
   */
  public float getValueLower() {
    return mValueLower;
  }

  /**
   * 设置低值
   *
   * @param lower 低值
   */
  public void setValueLower(float lower) {
    if (mValueLower != lower && mRangeLower <= lower && mRangeUpper >= lower) {
      mValueLower = lower;
      if (mOnValueChangedListener != null) {
        mOnValueChangedListener.onValueChanged(mValueLower, mValueUpper);
      }
      invalidate();
    }
  }

  /**
   * 获取高值
   */
  public float getValueUpper() {
    return mValueUpper;
  }

  /**
   * 设置高值
   *
   * @param upper 高值
   */
  public void setValueUpper(float upper) {
    if (mValueUpper != upper && mRangeLower <= upper && mRangeUpper >= upper) {
      mValueUpper = upper;
      if (mOnValueChangedListener != null) {
        mOnValueChangedListener.onValueChanged(mValueLower, mValueUpper);
      }
      invalidate();
    }
  }

  /**
   * 设置低值，悄悄的，不会引发监听器
   *
   * @param lower 低值
   */
  public void setValueLowerQuietly(float lower) {
    if (mValueLower != lower && mRangeLower <= lower && mRangeUpper >= lower) {
      mValueLower = lower;
      invalidate();
    }
  }

  /**
   * 设置高值，悄悄的，不会引发监听器
   *
   * @param upper 高值
   */
  public void setValueUpperQuietly(float upper) {
    if (mValueUpper != upper && mRangeLower <= upper && mRangeUpper >= upper) {
      mValueUpper = upper;
      invalidate();
    }
  }

  /**
   * 设置值
   *
   * @param lower 低进度
   * @param upper 高进度
   */
  public void setValues(float lower, float upper) {
    if (lower <= upper && mRangeLower <= lower && mRangeUpper >= upper) {
      mValueLower = lower;
      mValueUpper = upper;
      if (mOnValueChangedListener != null) {
        mOnValueChangedListener.onValueChanged(mValueLower, mValueUpper);
      }
      invalidate();
    }
  }

  /**
   * 设置值，悄悄的，不会引发监听器
   *
   * @param lower 低进度
   * @param upper 高进度
   */
  public void setValuesQuietly(float lower, float upper) {
    if (lower <= upper && mRangeLower <= lower && mRangeUpper >= upper) {
      mValueLower = lower;
      mValueUpper = upper;
      invalidate();
    }
  }

  /**
   * 设置进度改变监听器
   *
   * @param listener 监听器
   */
  public void setOnValueChangedListener(@Nullable OnValueChangedListener listener) {
    mOnValueChangedListener = listener;
  }

  public interface OnValueChangedListener {
    void onValueChanged(float lower, float upper);
  }
}
